/**
 * 
 */
package com.zhihu.search;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author zhoufox
 *
 */
public class Dao {
	
	public static final String NAME="com.mysql.jdbc.Driver";
	public static final String URL="jdbc:mysql://127.0.0.1:3306/crawler";
	public static final String USER="root";
	public static final String PASS="root";
	
	public Connection conn=null;
	public PreparedStatement pst=null;
	
	
	public Dao(String sql) {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String dburl = "jdbc:mysql://localhost:3306?useUnicode=true&characterEncoding=utf8";
			conn = DriverManager.getConnection(URL,USER,PASS);
			pst=conn.prepareStatement(sql);
			System.out.println("connection built");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void close(){
		try {
			this.conn.close();
			this.pst.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		String sql="select * from record";
		Dao dao = new Dao(sql);
		List<Record> list =new ArrayList<Record>();
		try {
			ResultSet ret=dao.pst.executeQuery();
			while (ret.next()) {
				Record record=new Record();
				record.setId((int) ret.getInt(1));
				record.setUrl((String)ret.getString(3));
				record.setCreateTime((Date)ret.getDate(9));
				System.out.println(record);
				list.add(record);
			}
		System.out.println(list);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
